package com.lxm.shownews.channel;


import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.lxm.shownews.bean.ChannelInfo;
import com.lxm.shownews.bean.News;
import com.lxm.shownews.bean.NewsPageBean;
import com.lxm.shownews.utils.JsonUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lxm on 2016/1/28.
 */
public class ChannelJsonUtils {

    public static List<ChannelInfo> readChannelBeans(String res) {
        List<ChannelInfo> channels = new ArrayList<>();
        JsonParser parser = new JsonParser();
        JsonObject jsonObj = parser.parse(res).getAsJsonObject();
        JsonArray jsonList = jsonObj.get("showapi_res_body")
                .getAsJsonObject().get("channelList").getAsJsonArray();
        for (int i = 0; i < jsonList.size(); i++) {
            JsonObject jo = jsonList.get(i).getAsJsonObject();
            ChannelInfo channelInfo = JsonUtils.deserialize(jo, ChannelInfo.class);
            channels.add(channelInfo);
        }
        return channels;
    }

    public static List<News> readNews(String res) {
        JsonParser parser = new JsonParser();
        JsonObject jsonObj = parser.parse(res).getAsJsonObject();
//        System.out.println("--showapi_res_code--" + jsonObj.get("showapi_res_code").getAsInt());
        JsonObject resObj = jsonObj
                .get("showapi_res_body").getAsJsonObject();
        JsonObject pageBeanObject = resObj.get("pagebean").getAsJsonObject();
        NewsPageBean deserialize = JsonUtils.deserialize(pageBeanObject, NewsPageBean.class);
//        System.out.println(deserialize.getAllNum() + "--------" + deserialize.getContentlist());
        return deserialize.getContentlist();
    }





}
